package org.hbhk.hxl.gateway.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {

	static RestTemplate re = new RestTemplate();

	public static String get(String url, String CASTGC) {

		try {
			HttpHeaders headers = new HttpHeaders();
			List<String> cookies = new ArrayList<>();
			cookies.add("CASTGC=" + CASTGC);
			headers.put(HttpHeaders.COOKIE, cookies);
			HttpEntity request = new HttpEntity(null, headers);
			ResponseEntity<String> result = re.exchange(url, HttpMethod.GET, request, String.class);

			return result.getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";

	}

	public static String json(String url, String json) {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
		String result = restTemplate.postForObject(url, formEntity, String.class);
		System.out.println(result);
		return result;

	}

}
